﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ole = AnalysisSystem.OleDbMethod;

namespace AnalysisSystem
{
    class MonthProduction
    {        
        public static DataTable GetMonthProduct(string wellName)
        {
            DataTable monthProduct = new DataTable();
            CreateTable(ref monthProduct);
            GetData(ref monthProduct, wellName);
            return monthProduct;
        }
        //建表
        private static void CreateTable(ref DataTable table)
        {
            table.TableName = "8-月产";
            string[] title = {"序号","井号", "生产日期",
                "平均油压（MPa）", "平均套压（MPa）",
                "累计产油（吨）", "累计产水（方）","累计产气（万方）",
                "产油天数", "产水天数", "产气天数"};
            for(int i=0; i<11; i++)
            {
                table.Columns.Add(title[i]);
            }
            table.Columns[2].DataType = typeof(DateTime);
            table.Rows.Add();
        }
        //获取相应数据并填充
        private static void GetData(ref DataTable table, string wellName)
        {
            DataTable wellDataReturn = new DataTable();
            var rows = from row in Ole.dataTables[1].AsEnumerable()
                       where row.Field<string>("井号").Equals(wellName)
                       select row;
            wellDataReturn = rows.CopyToDataTable<DataRow>();
            //获取循环的初始数据
            DateTime start = Convert.ToDateTime(wellDataReturn.Rows[0][3]);
            DateTime end = Convert.ToDateTime(wellDataReturn.Rows[wellDataReturn.Rows.Count - 1][3]);
            start = start.AddDays(-start.Day + 1);
            end = end.AddDays(-end.Day + 1);
            int count = 0;
            int serial = 0;
            //根据月份计算、填充数据
            for(var time = start; time<=end; time = time.AddMonths(1), serial++)
            {                
                double[] avePressure = new double[2];   //平均套压、油压
                double[] sumData = new double[3];       //累计产油、产水、产气
                int[] dayData = new int[5];       //套压、油压、产油、产水、产气天数                
                while(count < wellDataReturn.Rows.Count)
                {
                    DateTime tableTime = Convert.ToDateTime(wellDataReturn.Rows[count][3]);
                    if (tableTime.Year.Equals(time.Year) && tableTime.Month.Equals(time.Month))
                    {
                        for(int i=0; i<2; i++)
                        {
                            double value = ValueDataProcess(count, 4 + i, wellDataReturn);
                            avePressure[i] += value;
                            if (!value.Equals(0)) dayData[i] += 1;
                        }
                        for (int i = 0; i < 3; i++)
                        {
                            double value = ValueDataProcess(count, 6 + i, wellDataReturn);
                            sumData[i] += value;
                            if (!value.Equals(0)) dayData[2 + i] += 1;
                        }
                        count++;
                    }
                    else break;
                }
                FillData(serial, wellName, time, avePressure, sumData, dayData, ref table);
            }
        }
        //填充数据
        private static void FillData(int num, string well, DateTime time, double[] aveData, double[] sumData, int[] dayData, ref DataTable table)
        {
            table.Rows.Add();
            table.Rows[num][0] = num + 1;
            table.Rows[num][1] = well;
            table.Rows[num][2] = time;
            if (!dayData[0].Equals(0)) table.Rows[num][3] = Math.Round(aveData[0] / dayData[0], 2);
            else table.Rows[num][3] = DBNull.Value;
            if (!dayData[1].Equals(0)) table.Rows[num][4] = Math.Round(aveData[1] / dayData[1], 2);
            else table.Rows[num][4] = DBNull.Value;
            for(int i=0; i<3; i++)
            {
                table.Rows[num][i + 5] = Math.Round(sumData[i], 4);
                table.Rows[num][i + 8] = dayData[i + 2];
            }
        }
        //数据处理
        private static double ValueDataProcess(int row, int col, in DataTable table)
        {
            if (table.Rows[row][col].Equals(DBNull.Value))
                return 0;
            else return Convert.ToDouble(table.Rows[row][col]);
        }        
    }
}
